/*
	This file is part of "Ants on the web".

    "Ants on the web" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    "Ants on the web" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with "Ants on the web".  If not, see <http://www.gnu.org/licenses/>.
*/

package org.utc.ia04.antsontheweb.httprequest.agent;

import jade.core.AID;
import jade.core.Agent;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jade.wrapper.StaleProxyException;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.utc.ia04.antsontheweb.httprequest.behaviour.PoolManager;

public class HttpReqPoolManager extends Agent
{
	//serialVersionUID
	private static final long serialVersionUID = -783787290286442293L;
	
	//DF Information
	public static final String DF_TYPE = "HttpReqPoolManager";

	//Default param
	public static final int DEFAULT_NB_REQUESTER = 3;
	
	private int nbRequester;
	public List<AID> pool;
	
	protected void setup()
	{
		/* --- DEBUG ---
		System.out.println("PoolManager cree !");
		//-------------*/

		Object[] myArg = getArguments();
		nbRequester = (Integer)myArg[0];
		pool = new ArrayList<AID>();

		//Behaviours
		addBehaviour(new PoolManager());
		
		//Register
		register();
		
		//creation des HttpRequester
		AgentContainer c =  getContainerController();
		for (int i = 0; i<nbRequester; ++i)
			addRequester(c);
	}
	
	private void register()
	{
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());
		ServiceDescription sd = new ServiceDescription();
		sd.setType(DF_TYPE); 
		sd.setName(this.getName());
		dfd.addServices(sd);
		try { DFService.register(this, dfd); }
		catch (Exception fe) {}
	}
	
	public void removeRequester(AID requester)
	{
		//On retire l'ancien requester
		if(pool.remove(requester))
			addRequester(getContainerController());
	}
	
	public void addRequester(AgentContainer c)
	{
		try
		{	
			AgentController a = c.createNewAgent( "HttpRequester_" + UUID.randomUUID(), "org.utc.ia04.antsontheweb.httprequest.agent.HttpRequester",  null);
			a.start();
			
			pool.add(new AID(a.getName(), true));
		}
		catch (StaleProxyException e) { e.printStackTrace(); } 
	}
	
	public void takeDown()
	{
		/* --- DEBUG ---
		System.out.println("Pool dies !");
		//-------------*/
		
		DFAgentDescription dfd = new DFAgentDescription();
		dfd.setName(getAID());

		try { DFService.deregister(this, dfd); }
		catch (FIPAException e) { }
	}
}
